package com.hogan.hoganproject.base.api;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hogan.hoganproject.base.api.interceptor.CacheInterceptor;
import com.hogan.hoganproject.base.api.interceptor.ParamsInterceptor;
import com.hogan.hoganproject.base.app.HoganApplication;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Author: chenhu
 * Time: 2020/9/1 15:06
 * Description do somethings
 */
public class BaseHttpApi {
    private OkHttpClient.Builder mOkHttpClient;
    private static final int DEFAULT_TIME = 30;
    private Retrofit mBaseRetrofit;
    private Context mContext;
    /**
     * 设置缓存大小为100M
     */
    private static final int CACHE_SIZE = 1024 * 1024 * 100;

    public BaseHttpApi() {
        mContext = HoganApplication.getInstance().getApplicationContext();
        initOkHttpClient();
    }

    private static class SingHolder {
        private static BaseHttpApi INSTANCE = new BaseHttpApi();
    }

    public static BaseHttpApi getInstance() {
        return SingHolder.INSTANCE;
    }

    public Retrofit getRetrofit() {
        if (null == mBaseRetrofit) {
            mBaseRetrofit = new Retrofit.Builder()
                    .client(mOkHttpClient.build())
                    .addConverterFactory(GsonConverterFactory.create(getGson()))
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .baseUrl(ApiConstant.BASE_URL)
                    .build();
        }
        return mBaseRetrofit;
    }

    private Gson getGson() {
        Gson mGson = new GsonBuilder()
                .setLenient()
                .create();
        return mGson;
    }

    private void initOkHttpClient() {
        if (mOkHttpClient == null) {
            mOkHttpClient = new OkHttpClient.Builder();
        }
        mOkHttpClient.connectTimeout(DEFAULT_TIME, TimeUnit.SECONDS);
        mOkHttpClient.readTimeout(DEFAULT_TIME, TimeUnit.SECONDS);
        mOkHttpClient.writeTimeout(DEFAULT_TIME, TimeUnit.SECONDS);
        mOkHttpClient.cache(getCache(mContext))
                //    .addInterceptor(new HeadInterceptor())//根据需要设置，别乱设置
                .addInterceptor(new ParamsInterceptor())
                .addInterceptor(getHttpLoggingInterceptor())
                .addInterceptor(getCacheInterceptor(mContext))
                .addNetworkInterceptor(getCacheInterceptor(mContext))
                .build();
    }

    /**
     * 缓存位置
     */
    private Cache getCache(Context mContext) {
        File cacheFile = new File(mContext.getExternalCacheDir(), "HoganCache");
        return new Cache(cacheFile, CACHE_SIZE);
    }

    /**
     * 打印网络访问日志
     */
    private HttpLoggingInterceptor getHttpLoggingInterceptor() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        return loggingInterceptor;
    }

    /**
     * 缓存拦截器
     */
    private CacheInterceptor getCacheInterceptor(Context mContext) {
        return new CacheInterceptor(mContext, CACHE_SIZE);
    }
}
